package managedbean.SCM.materialRequest;

import entity.SCM.MaterialRequest;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import session.stateless.SCM.BatchBeanLocal;
import session.stateless.SCM.MaterialRequestBeanLocal;

@ManagedBean(name="viewMaterialRequestManagerBean")
@ViewScoped
public class ViewMaterialRequestManagerBean {

    public ViewMaterialRequestManagerBean() {
    }
    
    @EJB
    private MaterialRequestBeanLocal materialRequestBeanLocal;
    @EJB
    private BatchBeanLocal batchBeanLocal;
    private MaterialRequest selectedMaterialRequest;
    private String statusMessage;

    public MaterialRequest getSelectedMaterialRequest() {
        return selectedMaterialRequest;
    }

    public void setSelectedMaterialRequest(MaterialRequest selectedMaterialRequest) {
        this.selectedMaterialRequest = selectedMaterialRequest;
    }

    public String getStatusMessage() {
        return statusMessage;
    }

    public void setStatusMessage(String statusMessage) {
        this.statusMessage = statusMessage;
    }
    
    public List<MaterialRequest> getMaterialRequest(){
        return materialRequestBeanLocal.getMaterialRequest();
    }
    
    public List<MaterialRequest> getProcessMaterialRequest(){
        return materialRequestBeanLocal.getProcessMaterialRequest();
    }
    
    public List<MaterialRequest> getArriveMaterialRequest(){
        return materialRequestBeanLocal.getArriveMaterialRequest();
    }
    
    public List<MaterialRequest> getSuccessMaterialRequest(){
        return materialRequestBeanLocal.getSuccessMaterialRequest();
    }
    
    public void changeState(ActionEvent event){
        try{
            if(this.materialRequestBeanLocal.checkMaterialRequest(this.selectedMaterialRequest)){           
                boolean change = this.materialRequestBeanLocal.statePendingToArrived(this.selectedMaterialRequest.getMaterialRequestId());
                if(change){
                    statusMessage = "Material Request State has been changed successfully"; 
                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, statusMessage, ""));
                }else{
                    statusMessage = "State change Unsuccessfully";  
                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, statusMessage, ""));
                }
            }else{
                statusMessage = "Some Material Orders for this Material Request are not processed";
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, statusMessage, ""));
            }
            
        }catch(Exception ex){
            System.out.printf("Unable to change the state ");
        }
    }
    
    public void processMaterialRequest(ActionEvent event){
        try{
            
            boolean success1 = batchBeanLocal.processMaterialRequest(selectedMaterialRequest.getMaterialRequestId());
            boolean success2 = materialRequestBeanLocal.processArrivedMaterialRequest(selectedMaterialRequest.getMaterialRequestId());
            
            if(!(success1&&success2)){
                statusMessage = "Please Try Again.";
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Unsuccessful:" + statusMessage + "", ""));
            }else{
                statusMessage = "Peocess Material Request ID "+ selectedMaterialRequest.getMaterialRequestId() +" Successfully.";
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, statusMessage, ""));
            }
            
        }catch (Exception ex){
            ex.printStackTrace();
        }
    }
}
